/*
 * @Queue.java   1.0 2008/07/28
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.lightweightlog;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * This class is the place where we the Entries are stored, by default a Queue is cumulable
 * which means we want to first store all entries then a {@link QueueDumper} is subsequently called
 * to empty and unload each stored Entry to registered Dumper.
 * @author Leonardo Celati
 * @param <E> any extended Entry
 */
public abstract class Queue<E extends Entry> implements Constant, Cumulable {
    
    /**
     * true if this Queue is cumulable
     */
    private boolean cumulable;
    /**
     * Property holder
     */
    private Properties p;
    /**
     * Logger
     */
    private Log logger;
    
    /**
     * Default constructor
     * @param p custom set of properties
     */
    protected Queue(Properties p) {
        // assign property...
        this.p=p;
        // init log...
        logger = LogFactory.getLog();
        // deciding if this Queue is Cumulable or not
        // by looking at the interface implemented
        List<?> l = Arrays.asList(this.getClass().getInterfaces());
        if (l.contains(NotCumulable.class)) {
            cumulable=false; // no way this Queue is not cumulable
        } else {
            cumulable=true;
        }
        // clean up few resources
        l=null;        
    }
    
    /**
     * Get property from our property set...
     * @return value
     * @param name The key to look for
     */
    protected String get(String name) {
        for (Object key: p.keySet()) {
            if (((String)key).indexOf(Constant.PROP_ROOT + "queue." + name) > -1) {
                return ((String) p.get(key));
            }
        }
        // nothing found...
        return null;
    }
    
    /**
     * Inform us if this Queue is Cumulable or not. Default to true
     * @return true or false
     */
    protected final boolean isCumulable() {
        return cumulable;
    }
    
    /**
     * See if this cell is empty
     * @return true if it's empty, false if not
     */
    abstract boolean isEmpty();
    
    /**
     * Store the element into the Queue
     * @param e Any extended Entry
     * @return true if the element is correctly added to queue
     * @throws QueueException
     */
    abstract boolean push(E e)  throws QueueException;
    
    /**
     * Retrieve the actual size of this Queue
     * @return the size of this Queue
     */
    public abstract int size();
    
    /**
     * Overwriting the inherited method from Object
     * @return Valid string
     */
    @Override
    public String toString() {
        
        StringBuffer sb = new StringBuffer();
        
        for (Object key: p.keySet()) {
            if (((String)key).indexOf(Constant.PROP_ROOT + "queue." ) > -1) {
                sb.append( "," +  ((String)key).replaceAll(Constant.PROP_ROOT + "queue.", "") + "=" +  p.get(key) );
            }
        }
        //  returning...
        return super.toString() + " {hc=" + this.hashCode() + ",cumulable=" + this.cumulable + ",size=" + this.size() + sb + "}" ;
    }    

    
}


